const webpack = require('webpack');
const MegerRouterPlugin = require('./webpack-plugin/MegerRouterPlugin');

module.exports = () => {
    return {
        mode: 'development',
        entry: __dirname + '/src/main.ts', //入口文件
        output: {
            path: __dirname + '/dist', //打包文件的路径
            filename: 'bundle.js', //文件名称
        },
        resolve: {
            extensions: ['.ts', '.tsx', '.json', '.js']
        },
        devtool: 'source-map',
        devServer: {
            // host:"192.168.168.133",
			contentBase: './',
			port: 8080
		},
        module: {
            rules: [{
                test: /\.ts$/,
                exclude: /node_modules/,
                loader: 'ts-loader',
            }]
        },
        plugins: [
            // new ConsoleLogOnBuildWebpackPlugin(),
            // new CleanWebpackPlugin(),
            // new MegerRouterPlugin()
        ]
    }
}
class ConsoleLogOnBuildWebpackPlugin {
    pluginName = 'ConsoleLogOnBuildWebpackPlugin'
    apply(compiler) {
        // 代表开始读取records之前执行
        compiler.hooks.run.tap(this.pluginName, compilation => {
            console.log("webpack 构建过程开始！");
        });
    }
}
const fs = require("fs");
class CleanWebpackPlugin {
    pluginName = 'CleanWebpackPlugin'
    apply(compiler) {
        // 代表开始读取records之前执行
        compiler.hooks.emit.tap(this.pluginName, compilation => {
            console.log("webpack 开始删除文件！");
            this.delDir('./dist')
        });
    }
    delDir(path) {
        let files = [];
        if (fs.existsSync(path)) {
            files = fs.readdirSync(path);
            console.log('files', files)
            files.forEach((file, index) => {
                let curPath = path + "/" + file;
                if (fs.statSync(curPath).isDirectory()) {
                    this.delDir(curPath); //递归删除文件夹
                } else {
                    fs.unlinkSync(curPath); //删除文件
                }
            });
            // fs.rmdirSync(path);
        }
    }
}